/**
 * 报表设计公司(2011)
 */
package com.reportgear.designer.pane.common;

import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.ButtonGroup;
import javax.swing.JPanel;
import javax.swing.JRadioButton;

import com.reportgear.core.swing.pane.base.JBasePane;
import com.reportgear.report.core.cell.attr.CellExpandAttr;
import com.reportgear.report.core.cell.attr.CellExpandAttr.ConditionTransforDirection;
import com.reportgear.report.core.cell.attr.CellExpandAttr.ExpandDirection;
import com.reportgear.report.model.cell.DefaultCell;
import com.reportgear.report.view.grid.attr.editor.CellAttrEditorPaneInterface;

/**
 * 通用属性面板-单元格扩展与父格
 * 
 * @version 1.0 2011-3-28
 * @author <a herf="lexloo@gmail.com">lexloo</a>
 * @since ANNCSR 2.0
 */
public class JCellExpandAttrPane extends JBasePane implements CellAttrEditorPaneInterface {
	private ConditionTransDirectionPane	conditionTransPane;

	public JCellExpandAttrPane() {
		this.setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
		this.conditionTransPane = new ConditionTransDirectionPane();
		this.add(this.conditionTransPane);
	}

	@Override
	public void populate(DefaultCell cell) {
		CellExpandAttr attr = cell.getCellExpandAttr();
		this.conditionTransPane.setConditionTransDirection(attr.getConditionTransDirection());
	}

	@Override
	public void update(DefaultCell cell) {
		CellExpandAttr attr = new CellExpandAttr();

		attr.setConditionTransDirection(this.conditionTransPane.getConditionTransDirection());

		cell.setCellExpandAttr(attr);
	}

	// 扩展方向设置
	private static class ExpandDirectionPane extends JPanel {
		private JRadioButton	btnNone;
		private JRadioButton	btnLeftToRight;
		private JRadioButton	btnUpToDown;
		private ButtonGroup		group;

		private ExpandDirectionPane() {
			this.setBorder(BorderFactory.createTitledBorder("扩展方向"));
			group = new ButtonGroup();

			btnUpToDown = new JRadioButton("从上到下");
			this.add(btnUpToDown);
			group.add(btnUpToDown);

			btnLeftToRight = new JRadioButton("从左到右");
			this.add(btnLeftToRight);
			group.add(btnLeftToRight);

			btnNone = new JRadioButton("不扩展", true);
			this.add(btnNone);
			group.add(btnNone);
		}

		private void setExpandDirection(ExpandDirection direction) {
			switch (direction) {
			case None:
				btnNone.setSelected(true);
				break;
			case LeftToRight:
				btnLeftToRight.setSelected(true);
				break;
			default:
				btnUpToDown.setSelected(true);
				break;
			}
		}

		private ExpandDirection getExpandDirection() {
			if (btnLeftToRight.isSelected()) {
				return ExpandDirection.LeftToRight;
			} else if (btnUpToDown.isSelected()) {
				return ExpandDirection.UpToDown;
			} else {
				return ExpandDirection.None;
			}
		}
	}

	// 条件传递方向设置
	private static class ConditionTransDirectionPane extends JPanel {
		private JRadioButton	btnNone;
		private JRadioButton	btnLeftToRight;
		private JRadioButton	btnUpToDown;
		private ButtonGroup		group;

		private ConditionTransDirectionPane() {
			this.setBorder(BorderFactory.createTitledBorder("条件传递"));
			group = new ButtonGroup();

			btnUpToDown = new JRadioButton("从上到下");
			this.add(btnUpToDown);
			group.add(btnUpToDown);

			btnLeftToRight = new JRadioButton("从左到右");
			this.add(btnLeftToRight);
			group.add(btnLeftToRight);

			btnNone = new JRadioButton("不传递", true);
			this.add(btnNone);
			group.add(btnNone);
		}

		private void setConditionTransDirection(ConditionTransforDirection direction) {
			switch (direction) {
			case None:
				btnNone.setSelected(true);
				break;
			case LeftToRight:
				btnLeftToRight.setSelected(true);
				break;
			default:
				btnUpToDown.setSelected(true);
				break;
			}
		}

		private ConditionTransforDirection getConditionTransDirection() {
			if (btnLeftToRight.isSelected()) {
				return ConditionTransforDirection.LeftToRight;
			} else if (btnUpToDown.isSelected()) {
				return ConditionTransforDirection.UpToDown;
			} else {
				return ConditionTransforDirection.None;
			}
		}
	}

	public static void main(String[] args) {
		JCellExpandAttrPane p = new JCellExpandAttrPane();
		p.showDialog(null, "扩展属性", 300, 130);
	}
}
